--
-- @Author:      name
-- @DateTime:    2019-05-9 23:05:48
-- @Description: 消息处理

local skynet = require "skynet"


local MessageHandler = class("MessageHandler")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function MessageHandler:ctor(message_dispatch)

    self.node_cluster_id = skynet.getenv("node_cluster_id")
	self.message_dispatch = message_dispatch


	self:register()
	-- skynet.fork(function()
	-- 	self:checkHeartLoop()
	-- end)
end

--注册本服务里的消息
function MessageHandler:register()

	self.message_dispatch:registerSelf('start',handler(self,self.start))
	-- self.message_dispatch:registerSelf('connect',handler(self,self.onConnect))
	self.message_dispatch:registerSelf('disconnect',handler(self,self.onDisconnect))
	
end



--派发客户端消息，对返回消息处理
function MessageHandler:dispatchClientMessage(fd, address, message_id, message_name, message_body )
	skynet.ignoreret()	-- session is fd, don't call skynet.ret	
	-- skynet.trace() --追踪

	if not message_id or not message_name then
		--踢下线
		-- self.node_message:sendPoxy('kick_session', fd)
		return
	end
	local session = self.session_manager:getSession(fd)	
	print("________session___", session.user_id)
	local server_user = nil
	if session.user_id then 
		server_user = self.user_manager:getUser(session.user_id)
		if not server_user then 
			return 
		end
		message_body.user_id = server_user:getUserId()
	end
	local res = nil
	--根据message_id 区分是属于那个节点的消息		
	local index = math.floor(message_id / 1000)
	if index == 1 then 
		--网关登录消息
		res = self.message_dispatch:dispatchSelfMessage(message_name, fd, message_body)
	elseif index == 2 then
		--大厅消息			
		local server = server_user:getHallServer()
		if not server then 
			return 
		end
		local ok = nil
		ok, res = self.node_message:callNodeManager(server, message_name, message_body)
		if not ok then 
			error_log("___发送消息失败___", res)
			return
		end
	elseif index == 3 then
		--游戏消息			
		local server = server_user:getGameServer()
		if not server then 
			return 
		end
		local ok = nil
		ok, res = self.node_message:callNodeManager(server, message_name, message_body)
		if not ok then 
			error_log("___发送消息失败___", res)
			return
		end		
	end

	print("_______消息发往客户端_____",res)
	if res and type(res)=='table' and next(res) then 
		for message_name, body in pairs(res) do 
			if not body.error_code then 
				body.error_code = 0 --返回0代表请求处理成功
				body.error_msg = ""
			end
			self.node_message:sendClient(session.session_id, session.contype, message_name, body)
		end
	end
end


------------------------------------------------------------------------------cmd----------
--初始化
function MessageHandler:start()	
	self.node_message = global.node_message	
	self.user_manager = global.user_manager
	self.session_manager = global.session_manager
	self.login_handler = global.login_handler
	self.account_handler = global.account_handler
	self.session_manager:start()
	self.login_handler:start()
	self.user_manager:start()
	self.account_handler:start()
end

-- --socket连入
-- function MessageHandler:onConnect(conf)

-- 	local session = {
-- 		session_id = conf.fd,    	--socket句柄
-- 		contype = conf.contype,     --socket类型
-- 		service = conf.service,     --socket服务 agent.lua
-- 		addr = conf.addr,          	--ip地址
-- 		time = os.time(),          	--计时器，长时间没反应删除socket
-- 		sid = conf.sid or 1,        --消息验证id
-- 		user_id = nil,       		--用户id
-- 		gate = conf.gate,
-- 	}
-- 	self.session_manager:addSession(session)

-- 	--让agent.lua 把client消息转到这个服务来处理
-- 	skynet.call(conf.gate, "lua", "forward", conf.fd)	
-- 	print("MessageHandler:onConnect .........................")
-- 	return true
-- end

--断线
function MessageHandler:onDisconnect(fd, msg, err)		
	print("MessageHandler:onDisconnect .........................")
	self.session_manager:disconnect(fd)
end



return MessageHandler